package com.lemon.smartsafety.rayeye;

import android.os.Bundle;
import android.view.View;
import android.widget.TableRow;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lemon.android.http.LemonRequestParams;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppManager;
import com.lemon.smartsafety.rayeye.entity.BundleKey;
import com.lemon.smartsafety.rayeye.entity.FaultDetailVo;
import com.lemon.smartsafety.rayeye.util.RayType;
import com.lemon.smartsafety.rayeye.util.Urls;
import com.lemon.smartsafetynew.ui.activity.base.SwipeBackActivity;
import com.lemon.smartsafety.utils.app.HUDUtil;
import com.lemon.smartsafety.utils.common.DateUtils;
import com.lemon.smartsafety.utils.common.StringUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by Cooliean on 16/4/17.
 */
public class FaultDetailActivity extends SwipeBackActivity {


    @Bind(R.id.tv_building_name)
    TextView tvBuildingName;
    @Bind(R.id.tv_point_desc)
    TextView tvPointDesc;
    @Bind(R.id.row_point_desc)
    TableRow rowPointDesc;
    @Bind(R.id.tv_point_number)
    TextView tvPointNumber;
    @Bind(R.id.row_point_num)
    TableRow rowPointNum;
    @Bind(R.id.tv_warning_time)
    TextView tvWarningTime;
    @Bind(R.id.tv_fault_remark)
    TextView tvFaultRemark;
    @Bind(R.id.tv_status_desc)
    TextView tvStatusDesc;
    @Bind(R.id.tv_confirm_time)
    TextView tvConfirmTime;
    private int vId         = 0;
    private int mBuildingId = 0;
    private RayType mType;


    private String sUrl = "";
    private String mCompanyCode = "";

    @Override
    protected void getBundleArgs() {
        super.getBundleArgs();
        if (getIntent().getExtras() != null) {
            vId = getIntent().getExtras().getInt(BundleKey.V_ID, 0);
            mCompanyCode = getIntent().getExtras().getString(BundleKey.COMPANY_CODE, "");
            mBuildingId = getIntent().getExtras().getInt(BundleKey.BUILDING_ID, 0);
            mType = (RayType) getIntent().getExtras().getSerializable(BundleKey.TYPE);
        }

        mCompanyCode = StringUtils.nullValue(mCompanyCode);


    }

    @Override
    protected void initTitle() {
        setBarTitle("故障详情");
    }

    @Override
    protected void callFinish() {
        AppManager.getAppManager().finishActivity(FaultDetailActivity.class);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_fault_detail;
    }

    @Override
    protected void initViews() {
        super.initViews();
        if (vId == 0 || mBuildingId == 0 || mCompanyCode.length() == 0) {
            mHUDUtil.showErrorTip(getString(R.string.params_error), new HUDUtil.CallBack() {
                @Override
                public void onCall() {
                    callFinish();
                }
            });
        }
        sUrl = String.format(Urls.GET_FAULT_DETAIL, mSharedPreferencesHelper.getRayEyeUrl());
        getDetailInfo();
    }

    private void getDetailInfo() {
        LemonRequestParams params = new LemonRequestParams();
        params.put("code", mCompanyCode);
        params.put("key", Urls.APP_FIRE_SUPERVISON_KEY);
        params.put("id", vId);
        params.put("bid", mBuildingId);
        OkHttpUtils.post().url(sUrl).tag(sUrl).params(params).build().execute(new Callback<FaultDetailVo>() {
            @Override
            public void onBefore(Request request) {
                super.onBefore(request);
                if (mHUDUtil != null) mHUDUtil.showHUD();
            }

            @Override
            public FaultDetailVo parseNetworkResponse(Response response) throws Exception {
                return new Gson().fromJson(response.body().string(), getType());
            }

            @Override
            public void onError(Call call, Exception e) {
                if (mHUDUtil != null) {
                    mHUDUtil.showErrorTip(e.getLocalizedMessage());
                }
            }

            @Override
            public void onAfter() {
                super.onAfter();
            }

            @Override
            public void onResponse(FaultDetailVo response) {
                if (mHUDUtil != null) mHUDUtil.hidden();
                initDetailView(response);
            }
        });
    }

    private void initDetailView(FaultDetailVo vo) {
        if (vo != null && vo.getOb() != null) {
            tvBuildingName.setText(StringUtils.nullValue(vo.getOb().getBuildingName()));
            String pointDesc = StringUtils.nullValue(vo.getOb().getPointDesc());
            tvPointDesc.setText(StringUtils.nullValue(vo.getOb().getPointDesc(), "--"));
            if (pointDesc.length() == 0) {
                rowPointDesc.setVisibility(View.GONE);
            } else {
                rowPointDesc.setVisibility(View.VISIBLE);
            }
            String pointNum = StringUtils.nullValue(vo.getOb().getPointNumber());
            tvPointNumber.setText(StringUtils.nullValue(vo.getOb().getPointNumber(), "--"));
            if (pointNum.length() == 0) {
                rowPointNum.setVisibility(View.GONE);
            } else {
                rowPointNum.setVisibility(View.VISIBLE);
            }
            tvWarningTime.setText(DateUtils.getStringByFormat(vo.getOb().getLastTime(), DateUtils.dateFormatYMDHM));
            tvStatusDesc.setText(mType.getCharSeq());

            if (mType.getValue() == 0) {
                tvConfirmTime.setText(StringUtils.getStatusString("尚未处理", StringUtils.ColorStatus.STATUS_RED));
            } else {
                tvConfirmTime.setText(DateUtils.getStringByFormat(vo.getOb().getFlowTime(), DateUtils.dateFormatYMDHM));
            }


            tvFaultRemark.setText(StringUtils.nullValue(vo.getOb().getFaultDetail(), "--"));
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkHttpUtils.getInstance().cancelTag(sUrl);
    }
}
